package com.veal98.tree;

import com.veal98.bean.TreeNode;

public class BalancedBinaryTree_111 {

    private boolean res = true;

    public boolean isBalanced(TreeNode root) {
        getHeight(root);
        return res;
    }

    private int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }

        int leftHight = getHeight(root.left);
        int rightHeight = getHeight(root.right);

        if (Math.abs(leftHight - rightHeight) > 1) {
            res = false;
        }

        return Math.max(leftHight, rightHeight) + 1;
    }

}
